package com.example.shop.modules.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.shop.modules.sys.service.IUserAddressService;
import com.example.shop.persistence.entity.User;
import com.example.shop.persistence.entity.UserAddress;
import com.example.shop.utils.JSONResult;
import com.example.shop.utils.MPPageFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import static com.example.shop.utils.JSONResult.renderSuccess;

/**
 * <p>
 * 用户收货地址表 前端控制器
 * </p>
 *
 * @author jason
 * @since 2019-12-17
 */
@Api(tags = "收货地址")
@RestController
@RequestMapping("/sys/userAddress")
public class UserAddressController {

    @Autowired
    private IUserAddressService userAddressService;

    @ApiOperation(value = "新增或修改收货地址")
    @PostMapping("/add")
    public JSONResult addUserAddress(UserAddress userAddress){
        // 如果当前地址为默认地址
        if(userAddress.getIsDefault() == 1){
            // 将该用户其他地址全部设为非默认地址
            userAddressService.editIsDefault(userAddress.getUserID());
        }
        return renderSuccess(userAddressService.saveOrUpdate(userAddress));
    }

    @ApiOperation(value = "删除收货地址")
    @GetMapping("/delete")
    public JSONResult deleteUser(Integer id){
        return JSONResult.renderSuccess(userAddressService.removeById(id));
    }

    @ApiOperation(value = "收货地址列表", notes = "分页查询")
    @ApiImplicitParam(name="userID",value="用户ID")
    @GetMapping("/list")
    public JSONResult getUserAddress(Integer page, Integer pageSize, Integer userID){
        Page<UserAddress> pages = MPPageFactory.createPage(page,pageSize);
        QueryWrapper ew = new QueryWrapper();
        // 根据userID查询
        ew.eq("userID", userID);
        // 设置排序
        ew.orderByDesc("is_default");
        ew.orderByDesc("updateTime");
        IPage<UserAddress> list;
        if(userID == null){
            list = userAddressService.page(pages);
        }else{
            list = userAddressService.page(pages,ew);
        }
        return renderSuccess(list);
    }

    @ApiOperation(value = "默认收货地址查询", notes = "只查询一条记录")
    @GetMapping("/defAddress")
    public  JSONResult getDefaultUserAddress(Integer userID){
        QueryWrapper ew = new QueryWrapper();
        ew.eq("is_default", 1);
        ew.eq("userID", userID);
        UserAddress userAddress = userAddressService.getOne(ew, false);
        if(userAddress == null){
            // 如果没有指定默认收货地址  查询最近的一条记录
            QueryWrapper eq = new QueryWrapper();
            eq.eq("userID", userID);
            eq.orderByDesc("updateTime");
            eq.last("limit 1");
            userAddress = userAddressService.getOne(eq);
        }
        return renderSuccess(userAddress);
    }

}

